<template>
  <div>
    <el-dialog
      title="权限"
      :visible.sync="editDiavisible"
      width="70%"
      :before-close="cancel"
      @opened="beforeopen"
    >
      <el-tree
        :data="data"
        ref="tree"
        :props="props"
        default-expand-all
        show-checkbox
        @check-change="handleCheckChange"
        node-key="id"
        :default-checked-keys="ids"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="editSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAllMenu, addOrUpdate } from "@/api/family_doctor/family_doctor";

export default {
  data() {
    return {
      editDiavisible: false,
      data: [],
      ids: [],
      subData: {},
      menuData: true,
      props: {
        label: "name",
        children: "childrenNode",
      },
      param: {
        id: "",
        name: "",
        permissionReqList: [],
      },
    };
  },
  created() {},
  methods: {
    //关闭
    cancel() {
      this.editDiavisible = false;
      this.$emit("transData")
    },
    //弹窗渲染之前
    beforeopen() {},
    //提交
    editSubmit() {
      this.param.permissionReqList = this.$refs.tree
        .getCheckedNodes(false, true)
        .map((item) => ({ id: item.id, level: item.level }));
      addOrUpdate(this.param).then((res) => {
        if (res.code == "AA000000") {
          this.$message.success('分配成功！');
        }
        this.cancel();
      });
    },
    // 编辑提交事件
    async getCheckedNodes() {
      await this.$http.post("role/addOrUpdate", this.param1).then((res) => {
        this.dialogVisible = false;
        if (res.data.code == "AA000000") {
          this.$message.success(res.data.msg);
          this.getTableData();
          this.$http.post("menu/getAllMenu", this.params).then((ress) => {
            this.data = ress.data.data;
            this.ids = row.permissionIdList;
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    getMenuData() {
      getAllMenu(this.menuData).then((res) => {
        this.data = res.data;
      });
    },
    //供父组件调用函数
    openDialog(scope) {
      this.getMenuData();
      this.ids = scope.permissionIdList;
      this.param.id = scope.id;
      this.param.name = scope.name;
      this.editDiavisible = true;
    },
    // 节点选择事件
    handleCheckChange(data, checked) {
      if (data.level == 4) {
        this.subData = data;
			this.recursion(this.data, checked);
      }
    },
    // 递归
    recursion(data, checked) {
      data.forEach((item) => {
        if (item.level !== 3) {
          this.recursion(item.childrenNode);
        } else {
          if (this.subData.pId == item.id) {
            this.ids.push(item.id);
          }
        }
      });
    },
  },
};
</script>